package display;

import java.awt.Canvas;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

import entity.terrain.Terrain;

import level.*;

public class Game extends Canvas{
	public static final int WIDTH = 320;
	public static final int HEIGHT = 230;
	public BufferStrategy buffStrat;
	
	/* Environment variables */
	Level level;
	Manager lemmings;
	Terrain[] terrain;
	
	public Game() {
	JFrame frame = new JFrame("Lemmings");
		JPanel panel = (JPanel)frame.getContentPane();
		panel.setBackground(Color.black);
		setBounds(0,0,WIDTH,HEIGHT);
		panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
		panel.setLayout(null);
		panel.add(this);
		frame.setBounds(50,50,WIDTH,HEIGHT);
		frame.setVisible(true);
		frame.addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	public void init(){
		terrain = new Terrain[2];
		terrain[0] = new Terrain("images/terrain/ground.gif", 1, false, false);
		terrain[1] = new Terrain("images/terrain/obstacle.gif", 1, false, false);
		level = new Level(40, 180, 300, 200, 200, 1 ,terrain);
		lemmings = new Manager(1, level);
	}
	
	public void logic(){
		lemmings.play();
	}
		
	public void paint(Graphics g) {
		lemmings.draw(g);
	}
	
	
	public void run(){
		init();
		while (!lemmings.gameOver()) {
			logic();
			repaint();
			try { 
				 Thread.sleep(100);
			} catch (InterruptedException e) {}
		}
		System.out.println("GAMEOVER!!!");
	}
	
	public static void main(String[] args) {
		Game game = new Game();
		game.run();
	}
}